package com.fastwire.carriere.service;

import java.util.Arrays;
import java.util.List;

import javax.jws.WebService;

import com.fastwire.carriere.common.Messages;
import com.fastwire.carriere.common.util.CommonLogger;
import com.fastwire.carriere.common.util.ResourceUtil;
import com.fastwire.carriere.common.util.StringUtil;
import com.fastwire.carriere.controller.ControllerProxy;
import com.fastwire.carriere.controller.TopologicalLinkController;
import com.fastwire.carriere.controller.impl.TopologicalLinkControllerImpl;
import com.fastwire.carriere.inventory.vo.ConnectivityVo;
import com.fastwire.carriere.inventory.vo.TopologicalLinkVo;
import com.fastwire.carriere.service.dataconverter.XMLConverter;
import com.fastwire.carriere.service.webui.exception.ServiceException;
import com.fastwire.carriere.user.vo.SessionVo;

// TODO: Auto-generated Javadoc
/**
 * The Class TopologicalLinkInventoryServiceImpl.
 */
@WebService(endpointInterface = "com.fastwire.carriere.service.TopologicalLinkInventoryService")
public class TopologicalLinkInventoryServiceImpl implements
		TopologicalLinkInventoryService {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.TopologicalLinkInventoryService#
	 * getTopologicalLinkGraphbyNEIdAndHops(java.lang.String, int)
	 */
	@Override
	public String getTopologicalLinkGraphbyNEIdAndHops(String neIdStr,
			int hopNo, SessionVo sessionVo) throws ServiceException {

		CommonLogger.info(this, "getTopologicalLinksbyNEIdAndHops, neName:"
				+ neIdStr + ", hopNo is:" + hopNo);

		try {

			List<String> neIds = Arrays.asList(neIdStr.split(","));

			TopologicalLinkController tlController = (TopologicalLinkController) ResourceUtil
					.getSessionBindClassByName(
							TopologicalLinkControllerImpl.class.getName(),
							sessionVo, true);
			List<ConnectivityVo> tLinks = tlController
					.getTopologicalLinkGraphbyNEIdAndHops(neIds, hopNo);

			CommonLogger.info(this,
					"Total Topologial links is " + tLinks.size());

			return XMLConverter.serilizeObjectListToXML(tLinks);
		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.fastwire.carriere.service.TopologicalLinkInventoryService#
	 * getTopologicalLinksbyNEId(java.lang.String)
	 */
	@Override
	public List<TopologicalLinkVo> getTopologicalLinksbyNEId(String neId,
			SessionVo sessionVo) throws ServiceException {

		try {

			TopologicalLinkController tlController = (TopologicalLinkController) ResourceUtil
					.getSessionBindClassByName(
							TopologicalLinkControllerImpl.class.getName(),
							sessionVo, false);
			List<TopologicalLinkVo> tLinkVos = tlController
					.getTopologicalLinksbyNEId(neId);

			CommonLogger.info(this,
					"Total Topologial links is " + tLinkVos.size());

			return tLinkVos;

		} catch (Exception e) {
			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

	@Override
	public String updateTopologicalLink(TopologicalLinkVo tlVo,
			SessionVo sessionVo) throws ServiceException {
		String msg = Messages.getString("message.common.service.success");

		if (StringUtil.isEmptyString(tlVo.getRemarks())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "Remark" });
		}

		if (StringUtil.isEmptyString(tlVo.getNativeEmsName())) {
			return Messages.getString("message.common.error.nullempty",
					new String[] { "FDFr Native EMS Name" });
		}

		try {
			TopologicalLinkController tlController = (TopologicalLinkController) ResourceUtil
					.getSessionBindClassByName(
							TopologicalLinkControllerImpl.class.getName(),
							sessionVo, true);
			tlController.updateTopologicalLink(tlVo);
		} catch (Exception e) {

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

		return msg;

	}

	@Override
	public List<String> getTopologicalLinkUtilizations(String tlNameListStr,
			SessionVo sessionVo) throws ServiceException {

		try {

			List<String> tlNameList = Arrays.asList(tlNameListStr.split(","));
			TopologicalLinkController tlController = (TopologicalLinkController) ResourceUtil
					.getSessionBindClassByName(
							TopologicalLinkControllerImpl.class.getName(),
							sessionVo, true);
			return tlController.getTLUtilization(tlNameList);
		} catch (Exception e) {

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}
	}

	@Override
	public String getTunnelGraphbyTL(String tlNameListStr, SessionVo sessionVo)
			throws ServiceException {

		try {

			List<String> tlNameList = Arrays.asList(tlNameListStr.split(","));
			TopologicalLinkController tlController = (TopologicalLinkController) ResourceUtil
					.getSessionBindClassByName(
							TopologicalLinkControllerImpl.class.getName(),
							sessionVo, false);
			List<ConnectivityVo> conVos = tlController
					.getUtilizaingTunnels(tlNameList);

			return XMLConverter.serilizeObjectListToXML(conVos);
		} catch (Exception e) {

			String ex = ControllerProxy.getRootException(e);
			throw new ServiceException(ex);
		}

	}

}
